package cn.wy.system.service.impl;

import cn.wy.org.mapper.EmployeeMapper;
import cn.wy.system.domain.Role;
import cn.wy.system.mapper.RoleMapper;
import cn.wy.system.service.RoleService;
import cn.wy.basic.service.impl.BaseServiceImpl;
import org.junit.After;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public void insert(Role role) {
        super.insert(role);
        //添加当前角色的权限 - 前端一定是选择了权限
        if(role.getPermissions()!=null && role.getPermissions().size()>0){
            roleMapper.addRolePermission(role);
        }
        //添加当前角色的菜单 - 前端一定是选择了菜单
        if(role.getMenus()!=null && role.getMenus().size()>0){
            roleMapper.addRoleMenu(role);
        }
    }

    @Override
    public void delete(Long id) {
        //删除中间表：t_role_permission
        roleMapper.delPermissionByRoleId(id);
        //删除中间表：t_role_menu
        roleMapper.delMenuByRoleId(id);

        roleMapper.delEmployeeByRoleId(id);
        //删除角色
        super.delete(id);
    }

    @Override
    public void patchDel(List<Long> ids) {
        for (Long id : ids) {
            delete(id);
        }
    }

    @Override
    public void update(Role role) {
        super.update(role);
        //删除当前角色的所有权限
        roleMapper.delPermissionByRoleId(role.getId());
        //删除当前角色的所有菜单
        roleMapper.delMenuByRoleId(role.getId());
        //有外键约束，所以先删除关联数据，再删主表数据

        //添加当前角色的权限 - 前端一定是选择了权限
        if(role.getPermissions()!=null && role.getPermissions().size()>0){
            roleMapper.addRolePermission(role);
        }
        //添加当前角色的菜单 - 前端一定是选择了菜单
        if(role.getMenus()!=null && role.getMenus().size()>0){
            roleMapper.addRoleMenu(role);
        }


    }

}
